package com.yycx.module.bus.provider.handler.haiying.order;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.enums.HySaleTypeEnum;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 未申请开票订单列表
 *
 * @author zyf
 */
@Component
public class HyOrderTab3ListHandler implements PageInterceptor<HyOrder> {


    @Autowired
    private SysCompanyService companyService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        cq.eq("invoiceState", 0);
        OpenUser openUser = OpenHelper.getUser();
        String userName = openUser.getUsername();
        Long companyId = OpenHelper.getCompanyId();
        cq.eq("ho.companyId", companyId);
        cq.eq(HyOrder.class, "orderType", HySaleTypeEnum.CHA.getCode());
        if (!userName.contains("admin")) {
            cq.eq("ho.userId", openUser.getUserId());
        }
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrder> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            Long companyId = entityMap.get("companyId");
            if (FlymeUtils.isNotEmpty(companyId)) {
                SysCompany company = companyService.getById(companyId);
                entityMap.put("companyName", company.getCompanyName());
            }
        }
        return result;
    }

}
